$(function() {
	//设置活动按钮
	changeHeaderMenu(1);
	changeMenu(1, 1);
	//初始化列表数据
	initPoint();
});

function initPoint() {
	$("#add_card").change(function(){
		var point = $("#add_card option:selected").attr('point');
		if(point)
			$('.needPoint').html(point + "点数");
		else
			$('.needPoint').html("请选择充值卡时长");
	});
	$.getJSON('/fw/agent/agentbaseconfig_getbyid.action', function(data) {
		if(data.success && data.result) {
			var agentBase = data.result?data.result:600;
			agentBase = parseInt(agentBase);
			config.agentBase = agentBase;
			initPointSelect();
			// 获取主机数
			my_bind_check_num($('.host_allowed'));
			$('.host_allowed').bind('input propertychange', function() {
				var host_allowed = $(this).val();
				setPointSelect(host_allowed);
			});
			$('.host_allowed').val(200).trigger('input');
		} else {
		}
	});
}

var config = {
	agentBase: 600
}

function initPointSelect() {
	var agentBase = config.agentBase;
	var html = '';
	var p1 = parseInt(agentBase/12 * 1 * 1.5);
	html += '<option class="t" value="1" point="' + p1 + '">1个月（' + p1 + '点）</option>';
	var p2 = parseInt(agentBase/12 * 3 * 1.3);
	html += '<option class="t" value="3" point="' + p2 + '">3个月（' + p2 + '点）</option>';
	var p3 = parseInt(agentBase/12 * 6 * 1.1);
	html += '<option class="t" value="6" point="' + p3 + '">6个月（' + p3 + '点）</option>';
	html += '<option class="t" value="12" point="' + agentBase + '">1年（' + agentBase + '点）</option>';
	html += '<option class="t" value="24" point="' + (agentBase*2) + '">2年（' + agentBase*2 + '点）</option>';
	$('#add_card').append(html);
	$("#add_card").val(12).trigger('change');
}

function setPointSelect(host_allowed) {
	var agentBase = config.agentBase;
	var add = 0;
	host_allowed = parseInt(host_allowed);
	if(host_allowed >= 300) {
		add = Math.floor((host_allowed - 300) / 100);
		if(host_allowed % 100 >= 50)
			add += 1;
//		add = Math.floor(agentBase / 3 * add);
		add = 240 * add;
	}
	$('#add_card option').each(function(i, obj) {
		var v = $(obj).val();
		var p;
		var t;
		if(v == 1) {
			p = parseInt((add + agentBase)/12 * 1 * 1.5);
			t = '1个月（' + p + '点）';
		} else if(v == 3) {
			p = parseInt((add + agentBase)/12 * 3 * 1.3);
			t = '3个月（' + p + '点）';
		} else if(v == 6) {
			p = parseInt((add + agentBase)/12 * 6 * 1.1);
			t = '6个月（' + p + '点）';
		} else if(v == 12) {
			p = add + agentBase;
			t = '1年（' + p + '点）';
		} else if(v == 24) {
			p = (add + agentBase) * 2;
			t = '2年（' + p + '点）';
		} else {
			return;
		}
		$(obj).attr('point', p);
		$(obj).html(t);
	});
	$("#add_card").trigger('change');
}

function sure() {
	var host_allowed = $('.host_allowed').val();
	if(!host_allowed) {
		$('#alertdiv').savehandle('setmsg',{msg:'请输入主机数！',timer:5000});
		return;
	}
	if(host_allowed < 1) {
		$('#alertdiv').savehandle('setmsg',{msg:'主机数必须大于0！',timer:5000});
		return;
	}
//	if(host_allowed > 600){
//		$('#alertdiv').savehandle('setmsg',{msg:'主机数不能超过600，有疑问请联系官方！',timer:5000});
//		return;
//	}
	var time = $("#add_card").val();
	if(time == "0"){
		$('#alertdiv').savehandle('setmsg',{msg:'请选择续时时长！',timer:5000});
		return;
	}
	var varCode = $(".varCode").val();
	if(!varCode){
		$('#alertdiv').savehandle('setmsg',{msg:'请输入验证码！',timer:5000});
		return;
	}
	byCard();
}
var adding = false;
function byCard() {
	if(adding)
		return;
	adding = true;
	var host_allowed = $('.host_allowed').val();
	var time = $("#add_card").val();
	var varCode = $(".varCode").val();
    data = {
		'host': host_allowed,
    	'time': time,
	    'vcode': varCode
    };
    $.getJSON('/fw/agent/agentCon_addCard.action', data, function(data){
		if(data.success){
			if(data.result == "2"){
				$('#alertdiv').savehandle('setmsg',{msg:'生成失败,请确保剩余点数足够',timer:2000});
//				setTimeout(function() {
//					location.reload();
//				}, 2000);
			}else {
				$('#alertdiv').savehandle('setmsg',{msg:'生成成功,充值卡卡号为   : '+data.result+'  可在消费记录中查看!',
					timer:2000});
//				setTimeout(function(){
//					location.href = "recOfConsList.jsp";
//				}, 2000)
			}
		}else{
			$('#alertdiv').savehandle('setmsg',{msg:data.result.errors?data.result.errors:'生成失败', timer: 2000});
//			setTimeout(function() {
//				location.reload();
//			}, 2000);
		}
		$(".varCode").val('');
		changeVc();
		adding = false;
	});
}
//获取当前时间
function getDate() {
	var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    var milliseconds = date.getMilliseconds();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = year+month+strDate+milliseconds;
    return currentdate;
}
function changeVc(){
	$('.vcimg').attr('src','/fw/common/verify_graphCode.action?_dc' + Math.random());
}
